//
//  DHSubscription_CategoryView_CategoryCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/11.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSubscription_CategoryView_CategoryCell.h"
#import "DHSubscription_CategoryView_CategoryItemCell.h"
#import "DHProductListController.h"

@interface DHSubscription_CategoryView_CategoryCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *itemArr;

@end

@implementation DHSubscription_CategoryView_CategoryCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHSubscription_CategoryView_CategoryItemCell" bundle:nil] forCellWithReuseIdentifier:kDHSubscription_CategoryView_CategoryItemCell];
}

- (NSArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSArray array];
    }
    return _itemArr;
}

- (void)updateCellArray:(NSArray *)arr{
    self.itemArr = arr;
    [self.collectionView reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHSubscription_CategoryView_CategoryItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHSubscription_CategoryView_CategoryItemCell forIndexPath:indexPath];
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    cell.titleLabel.text = [model objectForKey:@"title"];
    [cell.imgView dh_setImageURLString:[model objectForKey:@"icon"]];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){(kScreenWidth - 20 - 15)/4,150};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHProductListController *vc = [[DHProductListController alloc] init];
    vc._id = [model objectForKey:@"_id"];
    vc.paramsKey = @"category_id";
    vc.navTitle = [model objectForKey:@"title"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

@end
